
/* SPDX-License-Identifier: GPL-2.0-only */

#ifndef __ACCOUNT_H
#define __ACCOUNT_H

# ifdef __cplusplus
extern "C" {
# endif
//content start
#include "sql.h"
#include <stdint.h>
//sql
int sql_exist(void *sql, const char *table, const char *field, const char *value);
//account 
enum account_status {
	ACCOUNT_STATUS_NULL		= 0,
	ACCOUNT_STATUS_ERROR_SIGN,
	ACCOUNT_STATUS_SIGN_NULL,
	ACCOUNT_STATUS_CONNECTING,
	ACCOUNT_STATUS_CONNECTED,
	ACCOUNT_STATUS_DISCONNECTED,
	ACCOUNT_STATUS_SUCCESS_SIGN,
	ACCOUNT_STATUS_SUCCESS_GET_CONTACTS,
	ACCOUNT_STATUS_SIGN_UP_SUCCESS,
	ACCOUNT_STATUS_SIGN_FAILED,

	ACCOUNT_STATUS_MSG_READ,
	ACCOUNT_STATUS_MSG_UNREAD,
};
enum account_list {
	ACCOUNT_ID,
	ACCOUNT_PWD,
	ACCOUNT_NICKNAME,
	ACCOUNT_EMAIL,
	ACCOUNT_CONTACT,
	ACCOUNT_IP,

	ACCOUNT_TMP1 = 0,//id
	ACCOUNT_PEER_ID,
	ACCOUNT_MSG,
	ACCOUNT_TIME,
	ACCOUNT_STATUS,
};
enum ACCOUNT_METHOD {
	ACCOUNT_METHOD_VERSION,		//0
	ACCOUNT_METHOD_VERSION_HANDLE,		//1

	ACCOUNT_METHOD_SIGN_IN,		//2
	ACCOUNT_METHOD_SIGN_IN_HANDLE,

	ACCOUNT_METHOD_SIGN_UP,		//4
	ACCOUNT_METHOD_SIGN_UP_HANDLE,

	ACCOUNT_METHOD_SIGN_UP_GET_ID,//6
	ACCOUNT_METHOD_SIGN_UP_GET_ID_HANDLE,

	ACCOUNT_METHOD_SIGN_UPDATE,	//8
	ACCOUNT_METHOD_SIGN_UPDATE_HANDLE,

	ACCOUNT_METHOD_GET_INFO,		//10
	ACCOUNT_METHOD_GET_INFO_HANDLE,

	ACCOUNT_METHOD_GET_OTHER_INFO,	//12
	ACCOUNT_METHOD_GET_OTHER_INFO_HANDLE,

	ACCOUNT_METHOD_GET_CONTACTS,	//14
	ACCOUNT_METHOD_GET_CONTACTS_HANDLE,

	ACCOUNT_METHOD_SEND_MSG,		//16
	ACCOUNT_METHOD_SEND_MSG_HANDLE,

	ACCOUNT_METHOD_GET_CONTACT_MSG,	//18
	ACCOUNT_METHOD_GET_CONTACT_MSG_HANDLE,

	ACCOUNT_METHOD_RESPONSE,	//20
	ACCOUNT_METHOD_END,
};
#define ACCOUNT_VERSION       "1.0.0"
#define ACCOUNT_TABLE_ACCOUNT "account"
#define ACCOUNT_FIELD_SOCKET	"socket"
#define ACCOUNT_FIELD_PEER_ID	"peer_id"
#define ACCOUNT_FIELD_ID	    "id"
#define ACCOUNT_FIELD_DATA	  "data"

const char *account_status_to_str(int status);
const char *account_status_to_button(int status);

void *account_json_init(const char *version);
int account_method_to_int(const char *src);
void *account_json_to_sql(void *sql, void *obj);
//sign_in
int account_sign_in_handle(void *sql, void *obj, void *user_data);
//sign_up_get_id
void *account_sign_up_get_id(void);
uint64_t account_sign_up_get_id_handle(void *sql);
//sign_up
int account_exist_id(void *sql, const char *id);
void *account_sign_up(const char *id, const char *pwd, const char *nickname,
		const char *email, const char *contact);
int account_sign_up_handle(void *sql, void *obj, void *socket);
//sign_update
void *account_sign_update(const char *id, const char *pwd,
    const char *nickname, const char *email, const char *contact);
int account_sign_update_handle(void *sql, void *obj_read);
//get info
int account_get_info_handle(void *sql, void *obj, void *obj_send);
int account_get_other_info_handle(void *sql, void *obj,	void *obj_send);
int account_get_contacts_handle(void *sql, void *obj,
		void *obj_send);
//msg
int account_json_send_msg_handle(void *socket, void *sql,
		void *obj, void *param);
int account_send_listener_msg(void *sql, const char *listener, const char *msg,
		const char *speaker, void *param);
void *account_json_get_contact_msg_handle(void *sql,
		void *obj_read, void *obj);
void *account_json_add_msg_from_sql(void *obj,
		void *sql, const char *id, const char *peer);
void *account_msg_json_analysis(void *obj, const char **speaker_p,
		const char **listener_p, const char **msg_p,
		int *status_p, uint64_t *time_p);

int account_get_limits(void *sql, const char *id, const char *listener);
void *account_get_socket(void *sql, const char *id);
size_t account_handle_msg(void *socket, const uint8_t *msg, size_t len,
		void* sql, void *param);
//client
/*
void *account_json_client_msg_handle(void *sql, void *obj_read);
void *account_client_msg_callback(const uint8_t *msg, void *socket,
		int *method, int *status);
		*/
int account_client_message_parse(const uint8_t *msg, int len,
		int *method, int *status, void **result_p);
void *account_client_sign_in(const char *id, const char *pwd);
void *account_client_get_contacts(const char *id);
void *account_client_get_contact_msg(const char *id, const char *peer_id);
void *account_client_send_msg(const char *id,
		const char *msg, const char *listener);
//content end
# ifdef __cplusplus
}
# endif

#endif //__ACCOUNT_H
